home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
clean
/
sun3.lha
/
Sun3
/
seqdemos
/
mulmat.icl
< prev
next >
Wrap
Text File
|
1992-08-07
|
3KB
|
92 lines
MODULE mulmat;
<<
Matrix Multiplication.
This program performs matrix multiplication on matrices of integers.
The initial matrices (Mat1 & Mat2) can have arbitrary size (Size). The
second matrix is transposed first, in order to avoid traversing a matrix
by column, which is very inefficient. The result of the program shows the
initial matrices and the resulting matrix.
>>
IMPORT deltaI;
TYPE
:: Row -> [INT]; == A row is a list of integers.
:: Col -> [INT]; == A column is a list of integers.
:: Mat -> [Row]; == A matrix is a list of rows.
:: TMat -> [Col]; == A transposed matrix is a list of columns.
:: Index -> INT; == An index is an integer.
MACRO
Size -> 6; == The size of the matrices.
RULE
== The initial matrices
:: Mat1 -> Mat;
Mat1 -> [[ 1, 2, 3, 4, 5, 6 ] ==
,[ 0, 1, 2, 3, 4, 5 ] ==
,[ -1, 0, 1, 2, 3, 4 ] == The product of these matrices:
,[ -2,-1, 0, 1, 2, 3 ] ==
,[ -3,-2,-1, 0, 1, 2 ] ==
,[ -4,-3,-2,-1, 0, 1 ]]; == [ 0 -9 0 5 1 7 ]
== [ 0 -8 -1 4 1 6 ]
:: Mat2 -> Mat; == [ 0 -7 -2 3 1 5 ]
Mat2 -> [[ 0, 1, 0, 0, 0,-1 ] == [ 0 -6 -3 2 1 4 ]
,[ 1, 0, 1, 1, 0, 1 ] == [ 0 -5 -4 1 1 3 ]
,[ -1, 0, 1,-1, 0, 0 ] == [ 0 -4 -5 0 1 2 ]
,[ -1,-1,-1, 0,-1, 0 ] ==
,[ 1, 0, 1, 0, 1, 0 ] ==
,[ 0,-1,-1, 1, 0, 1 ]]; ==
== Multiplying two matrices.
:: MulMat Mat Mat -> Mat;
MulMat m1 m2 -> TMulMat m1 (Transpose m2);
:: TMulMat Mat TMat -> Mat;
TMulMat [r] m2 -> [ MulRow r m2 ];
TMulMat [r|rs] m2 -> [ MulRow r m2 | TMulMat rs m2 ];
:: MulRow Row TMat -> Row;
MulRow r [c] -> [ Inprodukt r c ];
MulRow r [c|cs] -> [ Inprodukt r c | MulRow r cs ];
:: Inprodukt Row Col -> INT;
Inprodukt [] [] -> 0;
Inprodukt [a|as] [b|bs] -> + (* a b) (Inprodukt as bs);
== Transposing a matrix.
:: Transpose Mat -> TMat;
Transpose m -> Transp m 1;
:: Transp Mat Index -> TMat;
Transp m i -> [ Column m i ] , IF = i Size
-> [ Column m i | Transp m (++ i) ];
:: Column Mat Index -> Col;
Column [] i -> [];
Column [r|rs] i -> [ Select r i | Column rs i ];
:: Select Row Index -> INT;
Select [a|as] 1 -> a;
Select [a|as] i -> Select as (-- i);
== The Start rule: show the initial matrices and their product.
:: Start -> (Mat,STRING,Mat,STRING,Mat);
Start -> (m1,"\ntimes\n",m2,"\nbecomes\n",MulMat m1 m2),
m1: Mat1, m2: Mat2;